package com.example.shopping.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.shopping.pojo.*;
import com.example.shopping.pojo.vo.CarVo;
import com.example.shopping.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class IndexCon {
    @Autowired
    private TypeService typeService;
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private CarService carService;
    @Autowired
    private UserService userService;
    @Autowired
    private IndentService indentService;


    @GetMapping("/")
    public String userIndex(Model model){
        //添加类型列表
        List<Type> typeList = typeService.list();
        model.addAttribute("typeList",typeList);

        //添加商品列表
        List<Commodity> commodityList = commodityService.list();
        model.addAttribute(commodityList);


        return "userIndex";
    }

    /**
     * 传入指定类型，查找指定类型商品
     */
    @GetMapping("/typeListCommodity/{typeId}")
    @ResponseBody
    public List<Commodity> typeListCommodity(@PathVariable Integer typeId){
        QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
        commodityQueryWrapper.eq("type",typeId);
        return commodityService.list(commodityQueryWrapper);
    }

    /**
     * 进入详情页
     */
    @GetMapping("/details/{id}")
    public String details(@PathVariable Integer id,Model model){
        Commodity byId = commodityService.getById(id);
        model.addAttribute("details",byId);
        return "details";
    }

    /**
     * 购物车
     * <br>逻辑：在session中获取用户id
     * <br>在数据库中查询购物车中id为用户的值
     * <br>返回购物车list
     * @param session 获取值
     * @return
     */
    @GetMapping("/car")
    public String car(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        int id = user.getId();

        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        carQueryWrapper.eq("user_id",id);
        List<Car> list = carService.list(carQueryWrapper);
        ArrayList<CarVo> commodities = new ArrayList<>();
        for (Car car  : list){
            Integer commodityId = car.getCommodityId();
            Commodity byId = commodityService.getById(commodityId);
            CarVo carVo = new CarVo();
            carVo.setId(car.getId());
            carVo.setName(byId.getName());
            carVo.setPrice(""+byId.getPrice());
            carVo.setCommodityId(byId.getId());
            commodities.add(carVo);
        }
        model.addAttribute("commodities",commodities);
        return "car";

    }

    /**
     * 加入购物车<br>
     * 获取用户id<br>
     * 获取商品id<br>
     * 往购物车加入
     */
    @GetMapping("/addShoppingCar/{commodityId}")
    @ResponseBody
    public void addShoppingCar(@PathVariable Integer commodityId,HttpSession session){
        User user = (User)session.getAttribute("user");
        int id = user.getId();
        Car car = new Car(id, commodityId);
        carService.save(car);
    }

    /**
     * 余额购买<br>
     * 获取用户id<br>
     * 获取商品id<br>
     * 商品数量减一<br>
     * 用户余额减少对应数据<br>
     * 新建一个订单
     */
    @GetMapping("/balancePurchase/{commodityId}")
    @ResponseBody
    public void balancePurchase(HttpSession session,@PathVariable Integer commodityId){
        User user =(User) session.getAttribute("user");
        Commodity byId = commodityService.getById(commodityId);
        if (byId.getFlag()<=0 && user.getBalance()>byId.getPrice().intValue()){
            throw new RuntimeException("库存不足");
        }else {
            //商品数量减一
            UpdateWrapper<Commodity> commodityUpdateWrapper = new UpdateWrapper<>();
            commodityUpdateWrapper.set("flag",byId.getFlag()-1).eq("id",commodityId);
            commodityService.update(commodityUpdateWrapper);

            //用户余额减少
            int id = user.getId();
            user.setBalance(user.getBalance()+byId.getPrice().intValue());
            //用户积分变多
            user.setScore(user.getScore()+byId.getPrice().intValue()/10);
            userService.update(user);

            //订单加一 用户id 商品id 商品图片 商品名 商品详情
            Indent indent = new Indent(user.getId(), byId.getImg(), byId.getName(), byId.getDetails());
            indentService.save(indent);
            //更新session
            session.setAttribute("user",user);

        }
    }

    /**
     * 前往个人信息<br>
     */
    @GetMapping("/personalInformation")
    public String personalInformation(){
        return "personalcenter";
    }


    /**
     * <h2>积分购买</h2>
     * <p>获取用户id</p>
     * <p>获取商品id</p>
     * <p>商品数量减一</p>
     * <p>用户积分减少</p>
     */
    @GetMapping("/pointPurchase/{commodityId}")
    @ResponseBody
    public void pointPurchase(HttpSession session,@PathVariable Integer commodityId){
        User user =(User) session.getAttribute("user");
        Commodity byId = commodityService.getById(commodityId);
        if (byId.getFlag()<=0 && user.getScore()>byId.getPrice().intValue()){
            throw new RuntimeException("库存不足");
        }else {
            //商品数量减一
            UpdateWrapper<Commodity> commodityUpdateWrapper = new UpdateWrapper<>();
            commodityUpdateWrapper.set("flag",byId.getFlag()-1).eq("id",commodityId);
            commodityService.update(commodityUpdateWrapper);

            //用户积分变少
            user.setScore(user.getScore()-byId.getPrice().intValue());
            userService.update(user);

            //订单加一 用户id 商品id 商品图片 商品名 商品详情
            Indent indent = new Indent(user.getId(), byId.getImg(), byId.getName(), byId.getDetails());
            indentService.save(indent);
            //更新session
            session.setAttribute("user",user);
        }
    }

    /**
     * <h2>获取id</h2>
     * <p>根据id删除就可以啦</p>
     */
    @GetMapping("/removeCar/{id}")
    @ResponseBody
    public void removeCar(@PathVariable Integer id){
        carService.removeById(id);
    }


}
